var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    info: {},
    active: "",
    // more
    page: 1, // 分页数，当前页数
    isLoadMore: true, // 判断是否要加载更多的标志
    total_page: 0, // 总页数
    timer: null,
    nowTime: new Date().getTime() / 1000, // 现在的时间
    imgPath: app.globalData.imgPath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData(0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // 页面被被销毁的时候，清除定时器
    clearTimeout(this.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData(0);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      wx.showToast({
        title: "没有更多数据",
        icon: "none",
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getData(avtiveTag) {
    let json = {};
    console.log(avtiveTag);
    json.art = "buyList";
    if (avtiveTag == 0) {
      json.state = 1; // 拼购中
    } else if (avtiveTag == 1) {
      json.state = 2; // 拼购成功
    } else if (avtiveTag == 2) {
      json.state = 3; // 拼购失败
    }
    let urlStr = app.getNetAddresss("plugin.together-purchase.api.index.getTogetherData");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            total_page: res.data.buyList.last_page,
            isLoadMore: true,
            info: res.data.buyList.data,
            page: res.data.buyList.current_page,
          });
          if (!this.data.total_page) {
            this.setData({
              total_page: 0,
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
    });
  },
  gotoOrder(e) {
    let order_id = e.target.dataset.id || e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageA/member/orderdetail/orderdetail?order_id=" + order_id + "&orderType=groupsBuy",
    });
  },
  operation(e) {
    let btn = e.target.dataset.item || e.currentTarget.dataset.item;
    let order_id = e.target.dataset.order_id || e.currentTarget.dataset.order_id;
    if (btn.value == "together_purchase_pay") {
      this.toPay(order_id, btn);
    } else {
      this.publicMessage(btn, order_id);
    }
  },
  //按钮公共方法，点击提示框进行操作，确认后刷新
  publicMessage(btn, order) {
    console.log(btn);
    let that = this;
    if (!btn.api) {
      return;
    }
    wx.showModal({
      title: "提示",
      content: `确定${btn.name}?`,
      success(res) {
        if (res.confirm) {
          let urlStr = "";
          urlStr += app.getNetAddresss(btn.api);
          urlStr += "&order_id=" + order;
          app._getNetWork({
            url: urlStr,
            success: function (resdata) {
              var res = resdata.data;
              if (res.result == 1) {
                that.getdata();
                wx.showToast({
                  title: `${btn.name}成功`,
                  icon: "success",
                  duration: 1500,
                  success: function () {
                    // setTimeout(function() {
                    // 	//要延时执行的代码
                    // 	wx.navigateBack({
                    // 		delta: 1
                    // 	})
                    // }, 1500) //延迟时间
                  },
                });
              } else {
                wx.showToast({
                  icon: "none",
                  title: res.msg,
                  duration: 1000,
                });
              }
            },
            fail: function (res) {
              console.log(res);
            },
          });
        }
      },
    });
  },
  toPay(item, btn) {
    //status-2合并支付 type-1订单支付
    wx.navigateTo({
      url: "/packageD/buy/orderPay/orderPay?order_id=" + item + "&status=2" + "&api=" + btn.api,
    });
  },
  // 跳转主页
  gotoHome() {
    wx.reLaunch({
      url: "/packageG/index/index",
    });
  },
  //跳转拼购详情
  clickAdd(e) {
    let _id = e.target.dataset.id || e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageB/member/group_buy/group_buy_detail/group_buy_detail?" + "&id=" + _id,
    });
  },
  // 获取更多数据，加载更多
  getMoreData() {
    this.setData({
      isLoadMore: false,
    });
    let json = {};
    if (this.data.active == 0) {
      json.state = 1; // 正在拼团
    } else if (this.data.active == 1) {
      json.state = 2; // 拼团成功
    } else if (this.data.active == 2) {
      json.state = 3; // 拼团失败
    }
    json.art = "buyList";
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let pages = this.data.page;
      pages += 1;
      this.setData({
        page: pages,
      });
      json.page = pages;
      let urlStr = app.getNetAddresss("plugin.together-purchase.api.index.getTogetherData");
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          this.setData({
            isLoadMore: true,
          });
          var res = resdata.data;
          if (res.result == 1) {
            var nextPageData = res.data.buyList.data;
            let info = [...this.data.info, ...nextPageData];
            this.setData({
              info,
              page: res.data.buyList.current_page,
            });
          } else {
            let p = this.data.page;
            p -= 1;
            this.setData({
              page: p,
              isLoadMore: false,
            });
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
      });
    }
  },
  changeData(e) {
    this.setData({
      active: e.detail.index,
    });
    this.getData(e.detail.index);
  },
});
